//
//  RadioDetailViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/18.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "RadioDetailViewController.h"
#import "CFMacro.h"
#import "RadioDetailCollectionViewCell.h"
#import "GXNetTool.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "UIView+Frame.h"
// model
#import "RadioDetailRoot.h"
#import "RadioDetailPrograms.h"
#import "RadioDetailProgram_List.h"

#import "UIImageView+WebCache.h"

#define kRadioDetailCollectionViewCell @"RadioDetailCollectionViewCell"

@interface RadioDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *listener;
@property (weak, nonatomic) IBOutlet UILabel *location;
@property (weak, nonatomic) IBOutlet UILabel *type;
@property (nonatomic, strong) RadioDetailRoot *root;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UIView *menuView;
@property (weak, nonatomic) IBOutlet UILabel *pageTitle;

@end

@implementation RadioDetailViewController

#pragma mark - setter
- (void)setPlayKey:(NSInteger)playKey {
    _playKey = playKey;
    NSString *url = [NSString stringWithFormat:@"http://api.kufm.cn/program/channel_date_list3/%ld", playKey];
    [GXNetTool GET:url
           andBody:nil
         andHeader:nil
       andResponse:GXJSON
   andSuccessBlock:^(id result) {
       _root = [RadioDetailRoot modelWithDictionary:result];
       [self setUpSubViews];
       [self.collectionView reloadData];
   } andFailureBlock:^(NSError *error) {
       NSLog(@"电台详情VC数据请求失败");
   }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 69 - 20 - 40 - 49 - 64);
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"radioDetail"];
    kCollectionViewRegisterNib(@"RadioDetailCollectionViewCell", kRadioDetailCollectionViewCell);
}
#pragma mark - CollectionView 代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_root) {
        return _root.program_list.count;
    } else {
        return 5;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RadioDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kRadioDetailCollectionViewCell forIndexPath:indexPath];
    cell.program_list = _root.program_list[indexPath.row];
    return cell;
}
#pragma mark - scrollView 代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _lineView.gx_x = scrollView.contentOffset.x / 5.0;
    for (UIView *view in self.menuView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)view;
            [button setTitleColor:kDefaultColor forState:UIControlStateNormal];
        }
    }
}
#pragma mark - 给子控件赋值
- (void)setUpSubViews {
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:_root.channel_image_url] placeholderImage:[UIImage imageNamed:@"img_dj_head"]];
    _titleLabel.text = _root.channel_name;
    _listener.text = [NSString stringWithFormat:@"%ld", _root.listener_count];
    _type.text = _root.category;
    _location.text = _root.location;
}
#pragma mark - 返回按钮点击事件
- (IBAction)goBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
